﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.IO;

namespace playlist
{
    class PlayListsModel
    {
        #region PROPERTIES
        public string XmlLocation { get; set; }
        public Dictionary<string, PlayListModel> PlayListsDic { get; private set; }
        #endregion

        #region UPDATER
        public void AddInPlayList(int id, List<string> paths)
        {
            foreach (string path in paths)
            {
                //this.PlayListsDic.ElementAt(id).Value.MediaDic.Add(path, new MediaModel(new PlayListModel()/*this.PlayListsDic.ElementAt(id).Value*/, path));
                PlayListModel mm = this.PlayListsDic.ElementAt(id).Value;
                MediaModel tutu = new MediaModel(this.PlayListsDic.ElementAt(id).Value, path);
                if (!mm.MediaDic.ContainsKey(path))
                mm.MediaDic.Add(path, tutu);
            }
        }
        #endregion
        #region CTORS
        public PlayListsModel(string oo)
        {
            this.PlayListsDic = new Dictionary<string, PlayListModel>();
            try
            { this.Load(); }
            catch {
                this.AddDefaultPlaylist();
            }
        }
        #endregion

        #region LOADER SAVER
        public void Load()
        {

           XDocument loaded = XDocument.Load("./playlists.xml");

           var q = loaded.Element("playlists").Elements().Select(Playlist => new
           {
                    Name = (string) Playlist.Attribute("name"),
                    Medias = Playlist.Elements("media").Select(Medias => new {
                        Path = (string)Medias.Attribute("path")
                    })
               });

            foreach (var PlayList in q)
               {
                   PlayListsDic[PlayList.Name] = new PlayListModel();
                   PlayListsDic[PlayList.Name].MediaDic = new Dictionary<string, MediaModel>();
                   foreach (var Media in PlayList.Medias)
                       PlayListsDic[PlayList.Name].MediaDic[Media.Path] = new MediaModel(PlayListsDic[PlayList.Name], Media.Path);
               }
            if (PlayListsDic.Count() == 0)
                this.AddDefaultPlaylist();
        }

        public void Save()
        {
            XElement xml;
                
            xml = new XElement("playlists", "");

            foreach (KeyValuePair<string, PlayListModel> playlistsIt in this.PlayListsDic)
            {
                XElement concatPlayList;
                concatPlayList = new XElement("playlist",
                     new XAttribute("name", playlistsIt.Key)
                          );
            
                foreach (KeyValuePair<string, MediaModel> MediaIt in playlistsIt.Value.MediaDic)
                {
                    XElement concatMedia = new XElement("media", new XAttribute("path", MediaIt.Value.Location));
                    concatPlayList.Add(concatMedia);
                }
            xml.Add(concatPlayList);
            }
  
            try
            {
                xml.Save("./playlists.xml");
            }
            catch (Exception e)
            { }
        }

        public void AddDefaultPlaylist(int i = 0)
        {
            PlayListsDic["default playlist" + i] = new PlayListModel();
            PlayListsDic["default playlist" + i].MediaDic = new Dictionary<string, MediaModel>();
        }

        #endregion
    }
}
